package com.gmail.drheiniger.book.organizer;

import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

import com.gmail.drheiniger.backend.Database;

public class BrowseGUI implements ActionListener, FocusListener {

	// constants
	private static final String TITLE_lABEL_TEXT = "Browse Books";
	private static final String BACK_BUTTON_TEXT = "Back";
	private static final String DELETE_BUTTON_TEXT = "Delete";

	private JLabel titleLabel = new JLabel(TITLE_lABEL_TEXT);
	private JButton backButton = new JButton(BACK_BUTTON_TEXT);
	private JButton deleteButton = new JButton(DELETE_BUTTON_TEXT);
	private JFrame frame;
//	private int bookID;
	private Database db = new Database();
	JScrollPane browseScrollPane;
	private JTable table;

	public BrowseGUI(Database db) {
		//		System.out.println("got to browseGUI " + db.getPreparedStatement().toString());
		this.db = db;
//		bookID = 0;
		frame = new JFrame("Browse Books");
		Container contentPane = frame.getContentPane();
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		contentPane.setLayout(layout);
		db.createTable();
		table = db.getTable();
		browseScrollPane = new JScrollPane(table);
		titleLabel.setFont(new Font("Dialog", 1, 18));
		titleLabel.setOpaque(true);
		// titleLabel.setBackground(Color.BLACK); 
		titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
		titleLabel.setBorder(BorderFactory.createRaisedBevelBorder());

		//		browseScrollPane.addFocusListener(this);
		browseScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		browseScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		deleteButton.setEnabled(false);
		
		c.fill = GridBagConstraints.HORIZONTAL;
		//		c.ipadx = 80;
		c.ipady = 100;
		//		c.weighty = 1;
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = GridBagConstraints.REMAINDER;
		contentPane.add(titleLabel, c);

		//		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 275;
		c.gridx = 0;
		c.gridy = 1;
		contentPane.add(browseScrollPane, c);

		c.fill = GridBagConstraints.HORIZONTAL;

		//		c.weightx = 1;
		//		c.weighty = 1;
		c.weightx = 1;
		c.gridwidth = 1;
		c.ipady = 25;
		c.gridx = 0;
		c.gridy = 2;
		contentPane.add(deleteButton, c);

		c.gridx = 1;
		c.gridy = 2;

		contentPane.add(backButton, c);

		//		browseScrollPane.ac(Event.META_MASK, arg1)

		backButton.addActionListener(this);
		deleteButton.addActionListener(this);
		//		table.getComponent(table.getSelectedRow()).addFocusListener(this);
		table.addFocusListener(this);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 500);
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		frame.setVisible(true);

		checkForSelection();
	}

	//if this works at all, it will only call it once.  I need to implement a listener that is called every time something changes.
	public void checkForSelection(){
		if(this.table.hasFocus()){
			deleteButton.setEnabled(true);
		} else {
			deleteButton.setEnabled(false);
		}
	}

	public void actionPerformed(ActionEvent e) {

		Component source = null;
		String srcAction = "";
		source = (JButton) (e.getSource());
		srcAction = ((JButton) source).getText();
		switch (srcAction) {
		case BACK_BUTTON_TEXT:
			// go to add book page
			new OverviewGUI();
			frame.dispose();
			break;
		case DELETE_BUTTON_TEXT:
			int test = table.getSelectedRow();
			System.out.println(test);
			//			delete the book
			db.deleteRow(test);
			JOptionPane.showMessageDialog(frame, "Book Deleted");
			break;
		}
	}

	public int getBookID() {

		return 1;
	}


	@Override
	public void focusGained(FocusEvent e) {
		
		if(table.getSelectedRow() > -1){
			deleteButton.setEnabled(true);
		}
		//fix the focus problem.
		System.out.println(table.getSelectedRow());
		table.convertRowIndexToModel(table.getSelectedRow());
		System.out.println(table.convertRowIndexToModel(table.getSelectedRow()));
	}

	@Override
	public void focusLost(FocusEvent e) {
		deleteButton.setEnabled(false);
	}
}
